﻿using Biz.Core.Models;
using Biz.Core.Providers;
using MiniFox;
using MiniFox.Components;
using MiniFox.Infrastructure;

namespace Biz.Core.Services
{
    /// <summary>
    /// 业务实现
    /// </summary>
    [AutoResolve]
    public class DeviceService : IDeviceService
    {
        [AutoWired]
        public IDeviceDataProvider DataProvider
        {
            get; set;
        }

        public ResultObject<IEnumerable<DeviceVo>> QueryDeviceList(PagedQueryObject<SearchModel> queryObject)
        {
            return this.DataProvider.QueryDeviceList(queryObject);
        }

        public ResultObject<ViewTable> QueryDevices(PagedQueryObject<SearchModel> queryObject)
        {
            throw new NotImplementedException("暂不实现，样例不搞那复杂");
        }

        public DeviceVo CreateDevice(DeviceDto deviceDto)
        {
            deviceDto.DeviceId = SequenceGUID.NewGUID();// 非自增主键，需要创建带时间戳前缀的uuid
            if (this.DataProvider.CreateDevice(deviceDto))
            {
                // 创建成功后读取新数据
                return this.DataProvider.GetDevice(deviceDto.DeviceId);
            }
            return null;
        }

        public DeviceVo UpdateDevice(DeviceDto deviceDto)
        {
            if (this.DataProvider.UpdateDevice(deviceDto))
            {
                // 更新成功后读取新数据
                return this.DataProvider.GetDevice(deviceDto.DeviceId);
            }
            return null;
        }

        public string DeleteDevice(string deviceId)
        {
            if (this.DataProvider.DeleteDevice(deviceId))
            {
                // 删除成功后返回原id
                return deviceId;
            }
            return null;
        }

        public DeviceVo Toggle(string deviceId)
        {
            if (this.DataProvider.ToggleDevice(deviceId))
            {
                // 切换成功后读取新数据
                return this.DataProvider.GetDevice(deviceId);
            }
            return null;
        }

        public DeviceVo GetDevice(string deviceId)
        {
            return this.DataProvider.GetDevice(deviceId);
        }
    }
}
